﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Domain.Models;
using System.Data;
using Utilities;

namespace Domain.DAL
{
    public class RepairingTicketDAO
    {
        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public RepairingTicketDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public RepairingTicket Get(long id)
        {
            RepairingTicket theRepairingTicket = new RepairingTicket();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_Get", id);
                if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theRepairingTicket.ID = Convert.ToInt64(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["CustomerID"] != null && ds.Tables[0].Rows[0]["CustomerID"] != DBNull.Value)
                    {
                        theRepairingTicket.CustomerID = Convert.ToInt64(ds.Tables[0].Rows[0]["CustomerID"]);
                    }
                    if (ds.Tables[0].Rows[0]["ProductType"] != null && ds.Tables[0].Rows[0]["ProductType"] != DBNull.Value)
                    {
                        theRepairingTicket.ProductType = Convert.ToInt32(ds.Tables[0].Rows[0]["ProductType"]);
                    }
                    if (ds.Tables[0].Rows[0]["Manufacturer"] != null && ds.Tables[0].Rows[0]["Manufacturer"] != DBNull.Value)
                    {
                        theRepairingTicket.Manufacturer = Convert.ToInt32(ds.Tables[0].Rows[0]["Manufacturer"]);
                    }
                    if (ds.Tables[0].Rows[0]["Model"] != null && ds.Tables[0].Rows[0]["Model"] != DBNull.Value)
                    {
                        theRepairingTicket.Model = ds.Tables[0].Rows[0]["Model"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["SerialNumber"] != null && ds.Tables[0].Rows[0]["SerialNumber"] != DBNull.Value)
                    {
                        theRepairingTicket.SerialNumber = ds.Tables[0].Rows[0]["SerialNumber"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["ProductDescription"] != null && ds.Tables[0].Rows[0]["ProductDescription"] != DBNull.Value)
                    {
                        theRepairingTicket.ProductDescription = ds.Tables[0].Rows[0]["ProductDescription"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["CreatedDate"] != null && ds.Tables[0].Rows[0]["CreatedDate"] != DBNull.Value)
                    {
                        theRepairingTicket.CreatedDate = ds.Tables[0].Rows[0]["CreatedDate"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["ReceivingEmployee"] != null && ds.Tables[0].Rows[0]["ReceivingEmployee"] != DBNull.Value)
                    {
                        theRepairingTicket.ReceivingEmployee = Convert.ToInt32(ds.Tables[0].Rows[0]["ReceivingEmployee"]);
                    }
                    if (ds.Tables[0].Rows[0]["Note"] != null && ds.Tables[0].Rows[0]["Note"] != DBNull.Value)
                    {
                        theRepairingTicket.Note = ds.Tables[0].Rows[0]["Note"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"] != DBNull.Value)
                    {
                        theRepairingTicket.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theRepairingTicket;
        }

        public DataSet GetForPrintTemplate(string repairingTicketHistoryIDs)
        {
            RepairingTicket theRepairingTicket = new RepairingTicket();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_GetForPrintTemplate", repairingTicketHistoryIDs);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public DataSet GetForDetail(long id)
        {
            RepairingTicket theRepairingTicket = new RepairingTicket();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_GetForDetail", id);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public long InsertNewTicket(Customer c, RepairingTicket rt, RepairingTicketHistory rth)
        {
            long result = MyConfig.NullNumber;

            try
            {
                result = Convert.ToInt64(this.db.ExecuteScalar("sp_RepairingTickets_InsertNewTicket", c.FullName, c.Address, c.Mobile, c.HomePhone, rt.ProductType, rt.Manufacturer, rt.Model, rt.SerialNumber, rt.ProductDescription, rt.CreatedDate, rt.ReceivingEmployee, rt.Note, rth.Status, rth.RepairingType));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool UpdateNewTicket(Customer c, RepairingTicket rt, RepairingTicketHistory rth)
        {
            bool result = false;

            try
            {
                result = Convert.ToInt64(this.db.ExecuteScalar("sp_RepairingTickets_UpdatetNewTicket", c.FullName, c.Address, c.Mobile, c.HomePhone, rt.ProductType, rt.Manufacturer, rt.Model, rt.SerialNumber, rt.ProductDescription, rt.CreatedDate, rt.ReceivingEmployee, rt.Note, rth.ID, rth.RepairingType)) == rth.ID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public DataSet Search(string fullName, string mobile, string address, int productType, int manufacturer, string model, string serialNumber, string createdDateFrom, string createdDateTo, int repairingType, long status)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_ListTicketWithLastestStatus", fullName, mobile, address, productType, manufacturer, model, serialNumber, createdDateFrom, createdDateTo, repairingType, status);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public DataSet Search(int productType, string createdDateFrom, string createdDateTo, long status)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_ListTicketWithLastestStatusAndNoEndStatus", productType, createdDateFrom, createdDateTo, status);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public DataSet SearchForWarranty(string fullName, string mobile, string address, int productType, int manufacturer, string model, string serialNumber, string createdDateFrom, string createdDateTo, int repairingType, long status, string isValidWarranty)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_ListTicketWithLastestStatusForWarranty", fullName, mobile, address, productType, manufacturer, model, serialNumber, createdDateFrom, createdDateTo, repairingType, status, isValidWarranty);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public DataSet SearchForRepairQuality(string fullName, string mobile, string address, int productType, int manufacturer, string model, string serialNumber, string createdDateFrom, string createdDateTo, int repairingType, long status, string confirmedRepairQuality)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_ListTicketWithLastestStatusForRepairQuality", fullName, mobile, address, productType, manufacturer, model, serialNumber, createdDateFrom, createdDateTo, repairingType, status, confirmedRepairQuality);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public bool DeleteReceivingTicket(string repairingTicketHistoryID)
        {
            bool result = false;

            try
            {
                result = this.db.ExecuteScalar("sp_RepairingTickets_DeleleReceivingTicket", repairingTicketHistoryID).ToString() == repairingTicketHistoryID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public long InsertNewStatusForTicket(Customer c, RepairingTicket rt, RepairingTicketHistory rth, string updateReceivedInfo)
        {
            long result = MyConfig.NullNumber;

            try
            {
                result = Convert.ToInt64(this.db.ExecuteScalar("sp_RepairingTickets_InsertNewStatusForTicket", c.FullName, c.Address, c.Mobile, c.HomePhone, rt.ProductType, rt.Manufacturer, rt.Model, rt.SerialNumber, rt.ProductDescription, rt.CreatedDate, rt.ReceivingEmployee, rt.Note, rth.ID, rth.Repairer, rth.Status, rth.RepairingType, rth.CreatedDate, rth.Description, rth.Cost, rth.Note, rth.IsValidWarranty, rth.WarrantyNote, rth.WarrantyConfirmer, rth.RepairQualityID, rth.RepairQualityNote, rth.RepairQualityConfirmer, updateReceivedInfo));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool UpdateExistsStatusForTicket(Customer c, RepairingTicket rt, RepairingTicketHistory rth, string updateReceivedInfo)
        {
            bool result = false;

            try
            {
                result = Convert.ToInt64(this.db.ExecuteScalar("sp_RepairingTickets_UpdateExistsStatusForTicket", c.FullName, c.Address, c.Mobile, c.HomePhone, rt.ProductType, rt.Manufacturer, rt.Model, rt.SerialNumber, rt.ProductDescription, rt.CreatedDate, rt.ReceivingEmployee, rt.Note, rth.ID, rth.Repairer, rth.Status, rth.RepairingType, rth.CreatedDate, rth.Description, rth.Cost, rth.Note, rth.IsValidWarranty, rth.WarrantyNote, rth.WarrantyConfirmer, rth.RepairQualityID, rth.RepairQualityNote, rth.RepairQualityConfirmer, updateReceivedInfo)) == rth.ID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool UpdateWarrantyInfoForTicket(RepairingTicketHistory rth)
        {
            bool result = false;

            try
            {
                result = Convert.ToInt64(this.db.ExecuteScalar("sp_RepairingTickets_UpdateWarrantyInfoForTicket", rth.ID, rth.IsValidWarranty, rth.WarrantyNote, rth.WarrantyConfirmer, rth.WarrantyConfirmDate)) == rth.ID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public bool UpdateRepairQualityInfoForTicket(RepairingTicketHistory rth)
        {
            bool result = false;

            try
            {
                result = Convert.ToInt64(this.db.ExecuteScalar("sp_RepairingTickets_UpdateRepairQualityInfoForTicket", rth.ID, rth.RepairQualityID, rth.RepairQualityNote, rth.RepairQualityConfirmer, rth.RepairQualityConfirmDate)) == rth.ID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public DataSet ListAllHistories(long id)
        {
            RepairingTicket theRepairingTicket = new RepairingTicket();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTickets_ListAllHistories", id);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        #endregion

        #region Public Static Methods

        public static RepairingTicketDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<RepairingTicketDAO>();
        }

        #endregion
    }
}
